package org.example.java.jvm.gc;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * User: ZhangJuntao
 * Date: 14-2-8
 * Time: 下午10:37
 */
public class YoungGenGC {
  private static final int _1MB = 1024 * 1024;

  public static void main(String[] args) throws Exception {
    Map<Integer,String> map = new HashMap<>();
    int i = 0;
    while (true){
      map.put(i++,String.valueOf(i));
    }
//    testAllocation();
//    testPretenureSizeThreshold();
//    testHandlePromotion();
//    testTenuringThreshold();
//    testTenuringThreshold2();
  }

  /**
   * VM参数：-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
   */
  public static void testAllocation() throws Exception {
    byte[] allocation1, allocation2, allocation3, allocation4;
    allocation1 = (new byte[2 * _1MB]);
    System.out.println("1 allocate 4M");
    allocation2 = (new byte[4 * _1MB]);
    System.out.println("2 allocate 4M");
    allocation3 = (new byte[4 * _1MB]);
    Thread.sleep(5000);
    System.out.println("3 allocate 4M");
    TimeUnit.SECONDS.sleep(1);
    allocation4 = new byte[4 * _1MB];  // 出现一次Minor GC
    System.out.println("4 allocate 4M");
    TimeUnit.SECONDS.sleep(1);
    System.out.println(allocation1.length);
    System.out.println(allocation4.length);
  }
  public static void testAllocation1() throws Exception {
    byte[] allocation1, allocation2, allocation3, allocation4;
    WeakReference<byte[]> softReference1 = new WeakReference<byte[]>(new byte[4 * _1MB]);
    System.out.println("1 allocate 4M");
    WeakReference<byte[]> softReference2 = new WeakReference<byte[]>(new byte[4 * _1MB]);
    System.out.println("2 allocate 4M");
    SoftReference<byte[]> softReference3 = new SoftReference<byte[]>(new byte[4 * _1MB]);
    System.out.println("3 allocate 4M");
    TimeUnit.SECONDS.sleep(1);
    System.gc();
    allocation4 = new byte[4 * _1MB];  // 出现一次Minor GC
    System.out.println("4 allocate 4M");
    TimeUnit.SECONDS.sleep(1);
    System.out.println(softReference1.get());
    System.out.println(allocation4.length);
  }

  /**
   * VM参数：-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=3145728
   */
  public static void testPretenureSizeThreshold() {
    byte[] allocation;
    allocation = new byte[4 * _1MB];  //直接分配在老年代中
  }

  /**
   * VM参数：-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution
   */
  public static void testTenuringThreshold() {
    byte[] allocation1, allocation2, allocation3;
    allocation1 = new byte[_1MB / 4];  // 什么时候进入老年代决定于XX:MaxTenuringThreshold设置
    allocation2 = new byte[4 * _1MB];
    allocation3 = new byte[4 * _1MB];
    allocation3 = null;
    allocation3 = new byte[4 * _1MB];
  }

  /**
   * VM参数：-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=15  -XX:+PrintTenuringDistribution
   */
  public static void testTenuringThreshold2() {
    byte[] allocation1, allocation2, allocation3, allocation4;
    allocation1 = new byte[_1MB / 4];   // allocation1+allocation2大于survivor空间一半
    allocation2 = new byte[_1MB / 4];
    allocation3 = new byte[4 * _1MB];
    allocation4 = new byte[4 * _1MB];
    allocation4 = null;
    allocation4 = new byte[4 * _1MB];
  }

  /**
   * VM参数：-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:-HandlePromotionFailure
   */
  public static void testHandlePromotion() {
    byte[] allocation1, allocation2, allocation3, allocation4, allocation5, allocation6, allocation7;
    allocation1 = new byte[2 * _1MB];
    allocation2 = new byte[2 * _1MB];
    allocation3 = new byte[2 * _1MB];
    allocation1 = null;
    allocation4 = new byte[2 * _1MB];
    allocation5 = new byte[2 * _1MB];
    allocation6 = new byte[2 * _1MB];
    allocation4 = null;
    allocation5 = null;
    allocation6 = null;
    allocation7 = new byte[2 * _1MB];
  }
}
